package com.motionlog.activity;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.motionlog.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class WeiboImage extends Activity implements OnSeekBarChangeListener,OnClickListener{
    
	/** 从网络装载一张图片，改变其透明度
     *  */
	
	
	public static int alpha = 255;
	private MyView myView;
	private String[] jpgs={"1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg","7.jpg","8.jpg","9.jpg","10.jpg"};
	private int i=0;
	private SeekBar seekBar ;
	private Button btn ;
	private ImageView imageView;
    private int imageList = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weiboimage);
       
        //制定一个拖动条组件
        seekBar = (SeekBar) findViewById(R.id.seekBar1);
		seekBar.setMax(255);
		seekBar.setProgress(alpha);
		//邦定监听事件
		seekBar.setOnSeekBarChangeListener(this);
		//制定一个按钮
		
		btn = (Button) findViewById(R.id.buttonNext);
		btn.setOnClickListener(new View.OnClickListener() 
        {
            
            @Override
            public void onClick(View v) 
            { 
            	 String url = "http://sensen.duapp.com/gaoxiao/img/"+imageList+".jpg";
                 //得到可用的图片
                 Bitmap bitmap = getHttpBitmap(url);
                 //显示
                 imageView.setImageBitmap(bitmap);
                 imageList++;
            }
            });

		imageView = (ImageView)this.findViewById(R.id.weiboImageView);
       
		
        
    }

    
    
    /**
     * 获取网落图片资源 
     * @param url
     * @return
     */
    public static Bitmap getHttpBitmap(String url){
    	URL myFileURL;
    	Bitmap bitmap=null;
    	try{
    		myFileURL = new URL(url);
    		//获得连接
    		HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection();
    		//设置超时时间为6000毫秒，conn.setConnectionTiem(0);表示没有时间限制
    		conn.setConnectTimeout(6000);
    		//连接设置获得数据流
    		conn.setDoInput(true);
    		//不使用缓存
    		conn.setUseCaches(false);
    		//这句可有可无，没有影响
    		//conn.connect();
    		//得到数据流
    		InputStream is = conn.getInputStream();
    		//解析得到图片
    		bitmap = BitmapFactory.decodeStream(is);
    		//关闭数据流
    		is.close();
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	
		return bitmap;
    	
    }
    
    
    
    
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		alpha = progress;
		setTitle("当前透明度:" + progress);
		//重绘
		myView.invalidate();
	}

	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}

	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}
	
	
	//编写一个View子类实现图片显示
	private class MyView extends View{
		//使用方式1绘图
		private Bitmap bitmap;

		public MyView(Context context) {
			super(context);
			//注意：之前使用.png格式的图片不成功，后来改为.jpg成功了，不知什么原因
			java.io.InputStream is;
			try {
				System.out.println("bitmap已改变");
				is = getInputStreamFromUrl("http://sensen.duapp.com/gaoxiao/img/"+jpgs[i]);
				bitmap = BitmapFactory.decodeStream(is);
				setBackgroundColor(Color.WHITE);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}

		
		//重写onDraw方法
		@Override
		protected void onDraw(Canvas canvas) {
			System.out.println("重绘");
			Paint paint = new Paint();
			paint.setAlpha(alpha);
            //第二个参数表示复制的区域，第三个参数表示绘制目标区域
			canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap
					.getHeight()), new Rect(10, 10, 310, 335), paint);
		}
		
	}
	
	  //封装了一个方法，传入一个地址URL字符串，返回InputStream对象
    public InputStream getInputStreamFromUrl(String urlStr)
	throws MalformedURLException, IOException {
            URL url = new URL(urlStr);
            HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
            InputStream inputStream = urlConn.getInputStream();
            return inputStream;
}




	public void onClick(View v) {
	
	}
}